﻿// /////////////////////////////////// 
//  
//  
//     (c)Copyright 2001-2010 Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Collections.Specialized;

namespace CCF20Model
{
    
    public class DeviceCollection : ObservableCollection<Device>
    {
        private CCF20Entities ccf20Entities;
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            Device device = null;
            switch(e.Action)
            {
                case  NotifyCollectionChangedAction.Add:

                        device = (Device)e.NewItems[0];
                        ccf20Entities.AddToDevices(device);
                        break;
                case NotifyCollectionChangedAction.Remove:
                        device = (Device)e.OldItems[0];
                        ccf20Entities.DeleteObject(device);
                        //ccf20Entities.Devices.DeleteObject(device);
                        break;
            }
            ccf20Entities.SaveChanges();
            base.OnCollectionChanged(e);
        }
        protected override void InsertItem(int index, Device item)
        {
            
            if (item==null)
            {
                item = new Device();
                
            }
            item.IsManual = true;
            item.DeviceStatus = "库存";

            base.InsertItem(index, item);
            
        }
        public DeviceCollection(IQueryable<Device> devices, CCF20Entities context)
            : base(devices)
        {
            ccf20Entities = context;
        }

    }
    public class VehicleLicenseType
    {
        public int VehicleID { get; set; }
        public string VehicleLicense { get; set; }
    }

    public class DeviceModel
    {
        CCF20Entities ccf20Ent;
        public void Update()
        {
            ccf20Ent.SaveChanges();
        }
        public int GetAnchiID()
        {
            int? anchiID = ccf20Ent.Devices.Max(d => d.AnchiID);
            if (anchiID == null)
            {
                return 0;
            }
            else
            {
                return (int) anchiID;
            }
            
        }
        public string GetDeviceKeyBySimNumber(string sim)
        {
            TransferedCustomer tc = ccf20Ent.TransferedCustomers.Where(t => t.IDCNum == sim).FirstOrDefault();
            if (tc != null)
            {
                return tc.DeviceKey;
            }
            else
            {
                return string.Empty;
            }
        }
        
        public int GetCustomerIDByTransferSimNumber(string sim)
        {
            TransferedCustomer tc = ccf20Ent.TransferedCustomers.Where(t => t.IDCNum == sim).FirstOrDefault();
            if (tc != null)
            {
                return tc.Customer.CustomerID;
            }
            else
            {
                return 0;
            }
        }
        public int GetVehicleIDByTransferSimNumber(string sim)
        {
            TransferedCustomer tc = ccf20Ent.TransferedCustomers.Where(t => t.IDCNum == sim).FirstOrDefault();
            if (tc != null)
            {
                return tc.Customer.Vehicles.First().VehicleID;
            }
            else
            {
                return 0;
            }
        }
        
        public void AddDevice(Device device)
        {
            Device de = ccf20Ent.Devices.Where(d => d.IDCSimNumber == device.IDCSimNumber).FirstOrDefault();
            if (de != null)
            {
                LoggerHelper.LogCCF20(string.Format("不可添加重复的设备.IDCSimNumber={0}", device.IDCSimNumber));
                return;
            }
            ccf20Ent.Devices.AddObject(device);
            ccf20Ent.SaveChanges();
        }
        public bool IsDuplicateDevice(string searchValue,string searchField)
        {
            Device device = null;
            switch (searchField)
            {
                case "DeviceKey":
                    device = ccf20Ent.Devices.Where(d => d.DeviceKey == searchValue).FirstOrDefault();
                    break;
                case "IDCSimNum":
                    device = ccf20Ent.Devices.Where(d => d.IDCSimNumber == searchValue).FirstOrDefault();
                    break;
            }
            
            if (device == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public Customer GetCustomerByID(int ID)
        {
            Customer customer = ccf20Ent.Customers.Where(c => c.CustomerID == ID).FirstOrDefault();
            if (customer.Devices.Count == 0)
            {
                return customer;
            }
            else
            {
                throw new Exception("该客户已经与其它设备绑定，一个客户不可绑定多个设备");
            }
        }
        public DeviceCollection GetDevice()
        {
            var query = from c in ccf20Ent.Devices
                        orderby c.DeviceID
                        select c;
            return new DeviceCollection(query, this.ccf20Ent);
            
        }
        
        public void Delete(int DeviceID)
        {
            Device vdLocal = ccf20Ent.Devices.Where(vd => vd.DeviceID == DeviceID).First();
            ccf20Ent.Devices.DeleteObject(vdLocal);
            ccf20Ent.SaveChanges();
        }
        public DeviceModel()
        {
            ccf20Ent = new CCF20Entities();
        }
        public void UpdateConfirmTransferDateTime()
        {
            CompanyProfile cf = ccf20Ent.CompanyProfiles.FirstOrDefault();
            if (cf != null)
            {
                cf.ConfirmTransferDate = DateTime.Now;
            }

        }
        public DateTime? GetConfirmTransferDateTime()
        {
            CompanyProfile cf = ccf20Ent.CompanyProfiles.FirstOrDefault();
            if (cf != null)
            {
                return cf.ConfirmTransferDate;
            }
            else
            {
                return DateTime.Now;
            }


        }

    }
}
